package ru.ifmo.itmokis.util;

import java.awt.Color;
import java.awt.Dimension;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Ivan Stepuk
 */
public class ChartFactory {

    public static final int DEFAULT_WIDTH = 640;
    public static final int DEFAULT_HEIGHT = 480;

    public ChartPanel createSampleChart() {

        XYSeries a = new XYSeries("one");
        a.add(1, 1);
        a.add(2, 4);
        a.add(3, 9);
        a.add(4, 16);
        a.add(0, 0);

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(a);

        JFreeChart chart = org.jfree.chart.ChartFactory.createXYLineChart(
                "Sample",
                "x",
                "y",
                dataset,
                PlotOrientation.VERTICAL,
                false,
                false,
                false);

        return decorateChart(chart);
    }

    public ChartPanel createProductivityChart(XYSeriesCollection dataset) {

        JFreeChart chart = org.jfree.chart.ChartFactory.createXYLineChart(
                "Sample",
                "Time",
                "N",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                true);

        return decorateChart(chart);

    }

    public ChartPanel decorateChart(JFreeChart chart) {

        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);

        XYPlot plot = chart.getXYPlot();
        plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        plot.setBackgroundPaint(Color.BLACK);
        plot.setRenderer(renderer);

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));

        return chartPanel;
    }
}
